home *** CD-ROM | disk | FTP | other *** search
- // -*- Mode: C++ -*-
- // hippoplotXIV.cc -- Graphics code for producing X/InterViews displays.
- //
- // Copyright (C) 1991 The Board of Trustees of The Leland Stanford
- // Junior University. All Rights Reserved.
- //
- // $Header: /nfs/ebnextk/LocalSources/hippo/RCS/hippoplotXIV.cc,v 1.3 1992/01/07 02:51:08 pavel Rel $
- //
- // Author : Tom Pavel
- // Created On : Thu Jun 6 09:15:29 1991
- // Last Modified By: Tom Pavel
- // Last Modified On: Mon Jan 6 16:01:40 1992
- // Update Count : 22
- // Status : Revision 1
- //
-
-
- /*
- * Provides the equivalent of:
- * hippowraps.psw - histogramming package postscript wraps.
- * by william shipley, at SLAC, august 1990
- *
- * modified/maintained by mike gravina.
- *
- * updated to match new psw - Apr 13, 1991 pavel
- *
- * brought into the new mult-driver scheme - Jun 6, 1991 pavel
- *
- * new and improved mult-driver scheme - Sep 22, 1991 pavel
- *
- * minor improvements for release - 3-Jan-1992 pavel
- *
- */
-
-
- #include "hippoplotXIV.h"
-
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include <InterViews/canvas.h>
- #include <InterViews/painter.h>
- #include <InterViews/font.h>
- #include <InterViews/color.h>
- #include <InterViews/brush.h>
- #include <InterViews/raster.h>
- #include <InterViews/transformer.h>
-
-
- // These are for passing info from Histo class:
- Painter* h_plotter;
- Canvas* h_canvas;
-
- // All scaling info:
- static struct {
- float x;
- float y;
- Coord xMarg;
- Coord yMarg;
- float xOrig;
- float yOrig;
- } Scale;
-
- // The Data-coords limits:
- static struct {
- float xmin;
- float xmax;
- float ymin;
- float ymax;
- } Limit;
-
-
- inline Coord XScale (float x)
- { return Coord((x-Scale.xOrig)*Scale.x)+Scale.xMarg;}
-
- inline Coord YScale (float y)
- { return Coord((y-Scale.yOrig)*Scale.y)+Scale.yMarg;}
-
-
- inline Coord Convert (float d)
- // Use the InterViews global var "point" to scale properly for screen
- // resolution:
- { return Coord(d*point);}
-
-
- // Return a Color* corresponding to x. x is between 0 and 1
- static Color* getGrey(float x)
- {
- const int max = 65535; // = 0xffff
- const int fuzz = 0xff;
- int grey = int(x*max) & ~fuzz;
- return (new Color(grey,grey,grey));
- }
-
- static Color* getInvGrey(float x)
- {
- return getGrey(1-x);
- }
-
- static Color* getColor(float x)
- {
- // Well, ok this doesn't quite work; but it produces an interesting effect
- // anyway. Just goes to show how arbitrary color renderings are....
-
- const int max = 0xffffff;
- int rgb = int(x*max);
- int red = (rgb & 0xff0000) >> 8;
- int green = (rgb & 0x00ff00);
- int blue = (rgb & 0x0000ff) << 8;
- return (new Color(red,green,blue));
- }
-
-
-
-
- void initPlot_XIV(void* painter, void* canvas)
- {
- h_plotter = (Painter*) painter;
- h_canvas = (Canvas*) canvas;
- }
-
-
- void setHistoCoords_XIV(rectangle* margin, rectangle* data)
- {
- // Get windWidth and windHeight from the HistoView object; Don't
- // listen to what h_plot() tells us! [Unless, of course, the window
- // is unmapped, and we can't tell anyway]:
-
- if (h_canvas == 0) {
- fprintf(stderr, "setHistoCoords_XIV: Undefined Canvas\n");
- // return -1;
- return;
- }
-
-
- // Get overall width and height from h_canvas. Ignore drawRect.
- Coord windWidth = h_canvas->Width();
- Coord windHeight = h_canvas->Height();
-
- // Remember margin is in points; data is in data coords.
- Scale.x = Convert(margin->size.width)/(data->size.width);
- Scale.y = Convert(margin->size.height)/(data->size.height);
- Scale.xMarg = Convert(margin->origin.x);
- Scale.yMarg = Convert(margin->origin.y);
- Scale.xOrig = data->origin.x;
- Scale.yOrig = data->origin.y;
-
- Limit.xmin = data->origin.x;
- Limit.xmax = data->size.width + data->origin.x;
- Limit.ymin = data->origin.y;
- Limit.ymax = data->size.height + data->origin.y;
-
- #ifdef DEBUG
- printf ("setupXIV: X scale = %f, Y scale = %f\n", Scale.x, Scale.y);
- #endif
- }
-
-
-
- // Put the message text at the indicated x-y position (Note, x and y
- // are in points):
-
- void drawText_XIV(char *message,
- float x,
- float y,
- float fontHeight,
- float angle,
- char xalign,
- char yalign)
- {
- // label->SetFont() <something with fontSize = fontHeight>
-
- int fw = h_plotter->GetFont()->Width(message);
- int fh = h_plotter->GetFont()->Height();
-
- float radangle = angle*M_PI/180.0;
-
- x = Convert(x);
- y = Convert(y);
-
- switch (xalign) {
- case 'C':
- case 'c':
- x -= fw/2*cos(radangle);
- y -= fw/2*sin(radangle);
- break;
-
- case 'R':
- case 'r':
- x -= fw*cos(radangle);
- y -= fw*sin(radangle);
- break;
-
- case 'L':
- case 'l':
- default:
- break;
- }
-
- switch (yalign) {
- case 'C':
- case 'c':
- x += fh/2*sin(radangle);
- y -= fh/2*cos(radangle);
- break;
-
- case 'T':
- case 't':
- x += fh*sin(radangle);
- y -= fh*cos(radangle);
- break;
-
- case 'B':
- case 'b':
- default:
- break;
- }
-
- // Now write text in bg-fill-off mode (adding only the letters)
- boolean fill = h_plotter->BgFilled();
- h_plotter->FillBg(false);
-
- if (angle == 0 ) {
- // Get away with simple text:
- h_plotter->Text(h_canvas, message, Coord(x), Coord(y));
- } else {
- // Have to do some work:
- Transformer* t = new Transformer(h_plotter->GetTransformer());
-
- h_plotter->Rotate(angle);
- h_plotter->Translate(Coord(x),Coord(y));
-
- h_plotter->Text(h_canvas, message, 0, 0);
-
- h_plotter->SetTransformer(t); // restore old transform matrix.
- }
- h_plotter->FillBg(fill);
- }
-
-
-
- void drawMag_XIV(float x, float y,
- int mag, float fontsize)
-
- // x and y are in device coords (points).
- {
- // fontsize ignored for now...
- char string[10];
-
- Coord fw = h_plotter->GetFont()->Width("x10");
- Coord fh = h_plotter->GetFont()->Height();
-
- h_plotter->Text(h_canvas, "x10", Convert(x), Convert(y));
-
- h_plotter->Text(h_canvas, sprintf(string,"%d",mag),
- Convert(x)+fw, Convert(y)+fh/2);
- }
-
-
-
- void drawXTicks_XIV(float* x,
- int nt,
- float tickwidth,
- int side)
-
- {
- Coord yBorder;
- Coord width;
- if (side == 0) {
- yBorder = YScale(Limit.ymin);
- width = Convert(tickwidth);
- } else {
- yBorder = YScale(Limit.ymax);
- width = -1*Convert(tickwidth);
- }
-
- while (nt > 0) {
- h_plotter->Line(h_canvas,
- XScale(*x), yBorder,
- XScale(*x), yBorder+width);
- #ifdef DEBUG
- printf ("drawXTicks_XIV: tick %f at %d\n", *x, XScale(*x));
- #endif
-
- x++;
- nt--;
- }
- }
-
-
-
- void drawYTicks_XIV(float* y,
- int nt,
- float tickwidth,
- int side)
-
- {
- Coord xBorder;
- Coord width;
- if (side == 0) {
- xBorder = XScale(Limit.xmin);
- width = Convert(tickwidth);
- } else {
- xBorder = XScale(Limit.xmax);
- width = -1*Convert(tickwidth);
- }
-
- while (nt > 0) {
- h_plotter->Line(h_canvas,
- xBorder, YScale(*y),
- xBorder+width, YScale(*y));
- #ifdef DEBUG
- printf ("drawYTicks_XIV: tick %f at %d\n", *y, YScale(*y));
- #endif
-
- y++;
- nt--;
- }
- }
-
-
-
- void drawRect_XIV(float x, float y,
- float width, float height)
-
- // all args are in window coords
- {
-
- h_plotter->Rect(h_canvas,
- Convert(x), Convert(y),
- Convert(x+width),Convert(y+height));
- #ifdef DEBUG
- printf("drawRect_XIV: rect at (%d, %d) to (%d, %d)\n",
- Convert(x), Convert(y), Convert(x+width),Convert(y+height));
- #endif
- }
-
-
-
- void drawFilledRect_XIV(float x, float y,
- float width, float height,
- float grey)
-
- // all args are in window coords
- {
- Color* bg = h_plotter->GetBgColor();
- Color* fg = h_plotter->GetFgColor();
- Color* shade = getGrey(grey);
- h_plotter->SetColors(fg, shade);
-
- h_plotter->FillRect(h_canvas,
- Convert(x), Convert(y),
- Convert(x+width),Convert(y+height));
-
- h_plotter->SetColors(fg, bg);
-
- #ifdef DEBUG
- printf("drawFilledRect_XIV: rect at (%d, %d) to (%d, %d)\n",
- Convert(x), Convert(y), Convert(x+width),Convert(y+height));
- #endif
- }
-
-
-
- void shade_XIV(float xlow, float xhigh,
- float ylow, float yhigh )
-
- // all args are in data coords
- {
- const float grey = 0.6;
-
- Color* bg = h_plotter->GetBgColor();
- Color* fg = h_plotter->GetFgColor();
- Color* shade = getGrey(grey);
- h_plotter->SetColors(fg, shade);
-
- h_plotter->FillRect(h_canvas,
- XScale(xlow), YScale(ylow),
- XScale(xhigh), YScale(yhigh));
-
- h_plotter->SetColors(fg, bg);
-
- #ifdef DEBUG
- printf("drawFilledRect_XIV: rect at (%d, %d) to (%d, %d)\n",
- XScale(xlow), YScale(ylow), XScale(xhigh),YScale(yhigh));
- #endif
- }
-
-
-
-
- void drawLine_XIV(float *xy, int nxy, linestyle_t ls)
- {
- Coord* x = new Coord[nxy];
- Coord* y = new Coord[nxy];
-
- Coord* xp = x;
- Coord* yp = y;
- for (int i=0; i < nxy; i++) {
- *xp++ = XScale(*xy++);
- *yp++ = YScale(*xy++);
- }
-
- Brush* oldbrush = h_plotter->GetBrush();
- Brush* newbrush;
-
- switch (ls) {
- case SOLID:
- default:
- newbrush = new Brush(0xffff);
- break;
- case DASH:
- newbrush = new Brush(0x0f0f);
- break;
- case DOT:
- newbrush = new Brush(0x3333);
- break;
- case DOTDASH:
- newbrush = new Brush(0xf99f);
- break;
- }
-
- h_plotter->SetBrush(newbrush);
- h_plotter->Clip(h_canvas,
- XScale(Limit.xmin),YScale(Limit.ymin),
- XScale(Limit.xmax),YScale(Limit.ymax));
-
- h_plotter->MultiLine(h_canvas, x, y, nxy);
-
- h_plotter->SetBrush(oldbrush); // This will Unref() the newbrush.
- h_plotter->NoClip();
-
- delete x;
- delete y;
- }
-
-
-
-
- void drawPoints_XIV(float xy[],
- int nxy,
- int symbol,
- float symbolsize)
- {
-
- h_plotter->Clip(h_canvas,
- XScale(Limit.xmin),YScale(Limit.ymin),
- XScale(Limit.xmax),YScale(Limit.ymax));
-
- Coord ptsize = Convert(symbolsize);
-
- switch (symbol) {
- case SQUARE:
- default:
- while (nxy > 0) {
- h_plotter->Rect(h_canvas,
- XScale(*xy)-ptsize/2, YScale(*(xy+1))-ptsize/2,
- XScale(*xy)+ptsize/2, YScale(*(xy+1))+ptsize/2);
- xy += 2;
- nxy--;
- }
- break;
-
- case SOLIDSQUARE:
- while (nxy > 0) {
- h_plotter->FillRect(h_canvas,
- XScale(*xy)-ptsize/2, YScale(*(xy+1))-ptsize/2,
- XScale(*xy)+ptsize/2, YScale(*(xy+1))+ptsize/2);
- xy += 2;
- nxy--;
- }
- break;
-
- case PLUS:
- while (nxy > 0) {
- h_plotter->Line(h_canvas,
- XScale(*xy), YScale(*(xy+1))-ptsize/2,
- XScale(*xy), YScale(*(xy+1))+ptsize/2);
- h_plotter->Line(h_canvas,
- XScale(*xy)-ptsize/2, YScale(*(xy+1)),
- XScale(*xy)+ptsize/2, YScale(*(xy+1)));
- xy += 2;
- nxy--;
- }
- break;
-
- case TIMES:
- while (nxy > 0) {
- h_plotter->Line(h_canvas,
- XScale(*xy)-ptsize/2, YScale(*(xy+1))-ptsize/2,
- XScale(*xy)+ptsize/2, YScale(*(xy+1))+ptsize/2);
- h_plotter->Line(h_canvas,
- XScale(*xy)-ptsize/2, YScale(*(xy+1))+ptsize/2,
- XScale(*xy)+ptsize/2, YScale(*(xy+1))-ptsize/2);
- xy += 2;
- nxy--;
- }
- break;
- }
-
- h_plotter->NoClip();
- }
-
-
-
-
- void drawXError_XIV(float xy[],
- float errs[],
- int npts)
- {
- h_plotter->Clip(h_canvas,
- XScale(Limit.xmin),YScale(Limit.ymin),
- XScale(Limit.xmax),YScale(Limit.ymax));
-
- const Coord HALFERRWIDTH = 5;
- float y = 0;
- float xlo = 0;
- float xhi = 0;
-
- while (npts > 0) {
- xy++; // ignore x value
- y = *xy++;
- xhi = *errs++;
- xlo = *errs++;
-
- h_plotter->Line(h_canvas,
- XScale(xlo), YScale(y),
- XScale(xhi), YScale(y));
-
- h_plotter->Line(h_canvas,
- XScale(xlo), YScale(y-HALFERRWIDTH),
- XScale(xlo), YScale(y+HALFERRWIDTH));
-
- h_plotter->Line(h_canvas,
- XScale(xhi), YScale(y-HALFERRWIDTH),
- XScale(xhi), YScale(y+HALFERRWIDTH));
-
- npts--;
- }
-
-
- h_plotter->NoClip();
- }
-
-
- void drawYError_XIV(float xy[],
- float errs[],
- int npts)
- {
- h_plotter->Clip(h_canvas,
- XScale(Limit.xmin),YScale(Limit.ymin),
- XScale(Limit.xmax),YScale(Limit.ymax));
-
- const Coord HALFERRWIDTH = 5;
- float x = 0;
- float ylo = 0;
- float yhi = 0;
-
- while (npts > 0) {
- x = *xy;
- xy += 2;
- yhi = *errs++;
- ylo = *errs++;
-
- h_plotter->Line(h_canvas,
- XScale(x), YScale(ylo),
- XScale(x), YScale(yhi));
-
- h_plotter->Line(h_canvas,
- XScale(x-HALFERRWIDTH), YScale(ylo),
- XScale(x+HALFERRWIDTH), YScale(ylo));
-
- h_plotter->Line(h_canvas,
- XScale(x-HALFERRWIDTH), YScale(yhi),
- XScale(x+HALFERRWIDTH), YScale(yhi));
-
- npts--;
- }
-
-
- h_plotter->NoClip();
- }
-
-
-
- void drawColor2D_XIV(int nXBins,
- int nYBins,
- float minBin,
- float maxBin,
- float bins[])
- {
- const float xBinWidth = (Limit.xmax - Limit.xmin)/nXBins;
- const float yBinWidth = (Limit.ymax - Limit.ymin)/nYBins;
- float x = Limit.xmin;
- float y = Limit.ymin;
- const float scale = 1.0 / (maxBin - minBin);
-
- Color* bg = h_plotter->GetBgColor();
- Color* fg = h_plotter->GetFgColor();
- Color* shade;
-
- h_plotter->Clip(h_canvas,
- XScale(Limit.xmin),YScale(Limit.ymin),
- XScale(Limit.xmax),YScale(Limit.ymax));
-
- for (int i=0; i<nXBins; i++) {
- for (int j=0; j<nYBins; j++) {
- shade = getInvGrey(scale * (*bins++ - minBin));
- h_plotter->SetColors(shade, shade);
- h_plotter->FillRect(h_canvas,
- XScale(x), YScale(y),
- XScale(x+xBinWidth), YScale(y+yBinWidth));
-
- y += yBinWidth;
- }
- x += xBinWidth;
- y = Limit.ymin;
- }
-
- h_plotter->SetColors(fg, bg);
- h_plotter->NoClip();
- }
-
-
-
-
- void drawLego2D_XIV(int nXBins,
- int nYBins,
- float minBin,
- float maxBin,
- float bins[])
- {
- drawText_XIV("Not implemented yet.",
- XScale((Limit.xmin+Limit.xmax)/2),
- YScale((Limit.ymin+Limit.ymax)/2),
- h_plotter->GetFont()->Height(),
- 0,'c','c');
-
- }
-
-
-
-